using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using DIMP_Libraries;
using bomberbide_network;

namespace bomberbide_client
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        gameManager myGame;
        KeyboardState LastKeyboardState;

        double bomberId;

        ClientServeur myClient;

        public Game1(string Ip)
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            this.myClient = new ClientServeur(Ip, 44444);
            this.myClient.onMessageReceived += new EventHandler<MessageEventArg>(myClient_onMessageReceived);

            this.LastKeyboardState = Keyboard.GetState();
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            myGame = new gameManager(new camera(spriteBatch, new PrimitiveBatch(graphics.GraphicsDevice)), Content);
        }

        protected override void Update(GameTime gameTime)
        {
            if (Keyboard.GetState().IsKeyUp(Keys.Space) && this.LastKeyboardState.IsKeyDown(Keys.Space))
            {
                rQueryBombe lol = new rQueryBombe();
                lol.Position = this.myGame.Acteurs.Where(b => b.Id == this.bomberId).First().Position;
                this.myClient.SendMessage(lol);
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Q))
            {

                rQueryMovement lol = new rQueryMovement();
                lol.Movement = new Vector2(-1, 0);
                lol.Id = this.bomberId;
                this.myClient.SendMessage(lol);
            }
            if (Keyboard.GetState().IsKeyDown(Keys.S))
            {
                rQueryMovement lol = new rQueryMovement();
                lol.Movement = new Vector2(0, 1);
                lol.Id = this.bomberId;
                this.myClient.SendMessage(lol);
            }
            if (Keyboard.GetState().IsKeyDown(Keys.D))
            {
                this.myGame.Acteurs.Where(b => b.Id == this.bomberId).First().Vitesse = new Vector2(1, 0);
                rQueryMovement lol = new rQueryMovement();
                lol.Movement = new Vector2(1, 0);
                lol.Id = this.bomberId;
                this.myClient.SendMessage(lol);
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Z))
            {
                rQueryMovement lol = new rQueryMovement();
                lol.Movement = new Vector2(0, -1);
                lol.Id = this.bomberId;
                this.myClient.SendMessage(lol);
            }

            this.LastKeyboardState = Keyboard.GetState();

            myGame.Update(gameTime);

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            spriteBatch.Begin();
            this.myGame.Draw();
            spriteBatch.End();

            base.Draw(gameTime);
        }



        void myClient_onMessageReceived(object sender, MessageEventArg e)
        {
            if (typeof(ActionMessage) == e.Message.GetType())
            {
                ActionMessage caca = new ActionMessage();
                caca.Type = ActionType.BeginGame;
                this.myClient.SendMessage(caca);
            }
            if (typeof(rBombe) == e.Message.GetType())
            {
                cActeur toAdd = new cActeur();
                toAdd.Texture = Content.Load<Texture2D>("sprites/bombe");
                toAdd.Position = ((rBombe)e.Message).Position;
                toAdd.Id = ((rBombe)e.Message).Id;
                this.myGame.Acteurs.Add(toAdd);
            }
            if (typeof(rBomber) == e.Message.GetType())
            {
                if (this.myGame.Acteurs.Where(b => b.Id == ((rBomber)e.Message).Id).Count() == 0)
                {
                    cActeur toAdd = new cActeur();
                    toAdd.Texture = Content.Load<Texture2D>("sprites/dolan");
                    toAdd.Position = ((rBomber)e.Message).Position;
                    toAdd.Id = ((rBomber)e.Message).Id;
                    this.myGame.Acteurs.Add(toAdd);
                }
                else
                {
                    this.myGame.Acteurs.Where(b => b.Id == ((rBomber)e.Message).Id).First().Position = ((rBomber)e.Message).Position;

                }

            }
            if (typeof(rAssignBomber) == e.Message.GetType())
            {
                this.bomberId = ((rAssignBomber)e.Message).Id;
            }
            if (typeof(rMap) == e.Message.GetType())
            {
                this.myGame.Map = ((rMap)e.Message).Map;
            }
        }
    }
}
